<?php
/**
 * 模块管理  
 * ModuleController.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * -------------------------------
 * 
 * @package xcs
 * @author huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class ModuleController extends AdminController 
{	
    protected $oModel;	
	public function __construct($id, $module) {
		$this->oModel = new BaseModule();
		return parent::__construct($id, $module);
	}
	/**
	 * ModuleController::actionIndex() 模块首页
	 * 
	 * @return void
	 */
	public function actionIndex() {
		$aParams['pagesize'] = $this->pagesize;
		$aParams['page'] = $this->_request->getQuery('page' ,0);
		$aResult = $this->oModel->getList($aParams);
        $aResult['params'] = $aParams;	
		$this->render('index', $aResult);
	}
    /**
     * ModuleController::actionAdd() 添加
     * 
     * @return void
     */
    public function actionAdd(){
        if(isset($_POST['ajax']) && $_POST['ajax']==='module-_form-form'){
			echo CActiveForm::validate($this->oModel);
			Yii::app()->end();
		}
        $aPostData = $this->_request->getPost('BaseModule');
        if( $aPostData ){
            $aPostData['createtime'] = time();
            $aPostData['updatetime'] = time();
            $mResult = $this->oModel->saveData($aPostData);
            if( is_string($mResult) ){
                $alert="SendWebMsg('success','菜单添加成功','".$this->createUrl('module/index')."')";
            } else {
                $alert="SendWebMsg('error','保存失败，请修改页面提醒错误')";
            }
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
        }
        $this->render('_form' ,array('oModel' =>$this->oModel));
    }
    /**
     * ModuleController::actionEdit() 编辑
     * 
     * @return void
     */
    public function actionEdit(){
        $module = $this->_request->getQuery('module');
        $this->oModel = $this->oModel->findByPk($module);
        if(isset($_POST['ajax']) && $_POST['ajax']==='module-_form-form'){
			echo CActiveForm::validate($this->oModel);
			Yii::app()->end();
		}
        $aPostData = $this->_request->getPost('BaseModule');
        if( $aPostData ){
            $aPostData['updatetime'] = time();
            $mResult = $this->oModel->saveData($aPostData);
            if( is_string($mResult) ){
                $alert="SendWebMsg('success','菜单添加成功','".$this->createUrl('module/index')."')";
            } else {
                $alert="SendWebMsg('error','保存失败，请修改页面提醒错误')";
            }
            Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
        }
        $this->render('_form' ,array('oModel' =>$this->oModel));
    }
    /**
     * ModuleController::actionAjaxDisable() 禁用启用模块
     * 
     * @return json 保存数据后结果
     */
    public function actionAjaxDisable(){
        $aResulte = array();
        $module = $this->_request->getQuery('module');
        $this->oModel = $this->oModel->findByPk($module);
        $aData = $this->oModel->attributes;
        if( ! empty( $aData ) ){
            $aData['disable'] = $aData['disable']==1 ? 2 : 1;
            $sDisable = $aData['disable']==1 ? '禁用' : '启用';
            $mResult = $this->oModel->saveData($aData);
            $html = "<a href='javascript:void(0)' class='ajaxOperateRplace' url='".$this->createUrl('module/ajaxDisable' ,array('module'=>$module))."'>". $sDisable ."</a>";
            if( is_string($mResult) ){
                $aResulte = array(
                    'err' => 0 ,
                    'msg' => '状态更新成功',
                    'data' => array('html'=>$html)
                );
            } else {
                $aResulte = array(
                    'err' => 1 ,
                    'msg' => '更新状态失败'
                );
            }
        } else {
            $aResulte = array(
                'err' => 1 ,
                'msg' => '信息错误，可能原因是数据已经被删除了.'
            );
        }
        die(json_encode($aResulte));
    }
    /**
     * ModuleController::actionajaxSort() 排序
     * 
     * @return void
     */
    public function actionajaxSort(){
        $aResulte = array();
        $module = $this->_request->getQuery('module');
        $iSort = $this->_request->getQuery('sort');
        $this->oModel = $this->oModel->findByPk($module);
        $aData = $this->oModel->attributes;
        if( ! empty( $aData ) ){
            $aData['sort'] = $iSort;
            $mResult = $this->oModel->saveData($aData);
            if( is_string($mResult) ){
                $aResulte = array(
                    'err' => 0 ,
                    'msg' => '排序成功'
                );
            } else {
                $aResulte = array(
                    'err' => 1 ,
                    'msg' => '排序失败'
                );
            }
        } else {
            $aResulte = array(
                'err' => 1 ,
                'msg' => '排序失败，可能原因是数据已经被删除了.'
            );
        }
        die(json_encode($aResulte));
    }
    /**
     * ModuleController::actionDelete()
     * 
     * @return void
     */
    public function actionDelete(){
        $aResult = array();
        $module = $this->_request->getQuery('module');
        $this->oModel = $this->oModel->findByPk($module);
        if( $this->oModel ){
            $mResult = $this->oModel->deleteData($this->oModel);
            if( $mResult ){
                $aResulte = array(
                    'err' => 0 ,
                    'msg' => '删除成功'
                );
            } else {
                $aResulte = array(
                    'err' => 1 ,
                    'msg' => '删除成功失败'
                );
            }
        } else {
            $aResulte = array(
                'err' => 1 ,
                'msg' => '信息错误，可能原因是数据已经被删除了.'
            );
        }
        die(json_encode($aResulte));
    }
}
?>